import sqlite3

from yibao_orm.Field import IntegerField, StringField, IdField
from yibao_orm.yibao_db import Model


class User(Model):
    id = IdField()
    name = StringField('username')
    password = StringField('password')
    age = IntegerField('age')


if __name__ == '__main__':
    User.db = sqlite3.connect('db.sqlite')

    # 增
    User(name='Sundy', password='123456789009', age=27).save()
    User(name='John', password='99812121', age=33).save()
    User(name='Jock', password='981212', age=99).save()
    User(name='Jock1', password='9812121', age=99).save()

    # 删
    User().filter(name='Jock', password='981212')[0].delete()
    User().filter()[-1].delete()

    # 改
    User().filter(name='Sundy')[0].update(name='Sundy Lee', age=28)

    # 查
    print(User().filter(name='John', password=99812121)[0].id)
    print(User().filter(name='John')[0].name)
    print(User().filter()[0].name)
